<template>
  <div class="user">
    <div class="search">
      <PageSearch
        :searchFormConfig="searchFormConfig"
        @resetBtnClick="handleResetClick"
        @queryBtnClick="handlequeryBtnClick"
      >
      </PageSearch>
    </div>
    <div class="content">
      <page-content
        ref="pageContentRef"
        :contentTableConfig="contentTableConfig"
        pageName="users"
        @editBtnClick="handleEditData"
        @newBtnClick="handleNewData"
      >
        <template #enable="scope">
          <el-button :type="scope.row.enable ? 'success' : 'danger'" plain>
            <!-- scope.row 就是拿到插槽里面 ：row 对应的值 -->
            {{ scope.row.enable ? "启用" : "禁用" }}
          </el-button>
        </template>
      </page-content>
    </div>
    <pageModal
      :defaultInfo="defaultInfo"
      :modalConfig="modalConfigRef"
      ref="pageModalRef"
      pageName="users"
    ></pageModal>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from "vue";

import { searchFormConfig } from "./config/search.config";
import PageSearch from "@/components/page-search";
import PageContent from "@/components/page-content";
import pageModal from "@/components/page-modal";
import { contentTableConfig } from "./config/content.config";
import { usePageSearch } from "@/hooks/usePageSearch";
import { modalConfig } from "@/views/main/system/user/config/modal.config";
import { usePageModal } from "@/hooks/use-page-modal";
import { useStore } from "@/store";

export default defineComponent({
  name: "user",
  components: {
    PageSearch,
    PageContent,
    pageModal,
  },
  setup() {
    const [handlequeryBtnClick, handleResetClick, pageContentRef] =
      usePageSearch();

    // pagemodal相关hook逻辑
    const newCallbacks = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === "password"
      );
      passwordItem!.isHidden = false;
    };
    const editCallbacks = () => {
      const passwordItem = modalConfig.formItems.find(
        (item) => item.field === "password"
      );
      passwordItem!.isHidden = true;
    };

    // 2、动态添加部门和角色列表
    const store = useStore();

    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find(
        (item) => item.field === "departmentId"
      );
      departmentItem!.options = store.state.entireDepartments.map((item) => {
        return { title: item.name, value: item.id };
      });
      const roleItem = modalConfig.formItems.find(
        (item) => item.field === "roleId"
      );
      roleItem!.options = store.state.entireRole.map((item) => {
        return { title: item.name, value: item.id };
      });
      return modalConfig;
    });

    // 3、调用hook获取公共的变量和函数
    const [pageModalRef, defaultInfo, handleEditData, handleNewData] =
      usePageModal(newCallbacks, editCallbacks);

    return {
      searchFormConfig,
      contentTableConfig,
      handlequeryBtnClick,
      handleResetClick,
      pageContentRef,
      // modalConfig,
      modalConfigRef,
      handleEditData,
      pageModalRef,
      defaultInfo,
      handleNewData,
    };
  },
});
</script>

<style scoped lang="less">
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #ffffff;
  .el-table {
    display: flex;
    justify-content: center;
  }
}
</style>
